// 网格仓
import { PopupSelection } from '@/components/el/ItemComponent';
import React from 'react';
import requests from '@/utils/request';
import { ElNotification } from '@/components/el';
interface Props {
  value?: any;
  onChange?: Function;
  paramData?: any;
}
interface State {
  popupSelection: any;
}
// 在此处组装所有的数据
class CoopPartnerPopupSelection extends React.Component<Props, State> {
  static defaultProps = {
    paramData: {}
  };
  /**
   * @description 构造器
   */
  constructor(props) {
    super(props);
    this.state = {
      popupSelection: null // 通过ref获取inputValue   popupSelection.state.inputValue
    };
  }

  onRef = (ref) => {
    this.setState({
      popupSelection: ref
    });
  };

  componentDidMount() {
    this?.state?.popupSelection?.setSearchFormData({
      type: this?.props?.paramData?.ptype || 'EMP'
    });
  }

  // 分页查询requests
  getList = (data: any) => {
    switch (data.type || this.props.paramData.ptype) {
      case 'CUST':
        return requests('/yst-sale/crm/cust/search', {
          method: 'post',
          query: {
            ...data,
            custCode: data.pcode,
            custName: data.pname,
            pname: null,
            pcode: null,
            orders: [{ asc: false, column: 'createTime' }]
          }
        });
      case 'EMP':
        return requests('/yst-support/org/component/emp/paging', {
          method: 'post',
          query: {
            ...data,
            empCodeName: data.pcode || data.pname,
            pname: null,
            pcode: null,
            // apprStatus: 'APPROVED',
            orders: [{ asc: false, column: 'createTime' }],
            ...this.props.paramData
          }
        });
      case 'SUPP':
        return requests('/yst-pur/pur/purSupp/suppSearch', {
          method: 'post',
          query: {
            ...data,
            suppCode: data.pcode,
            suppName: data.pname,
            pname: null,
            pcode: null,
            // apprStatus: 'APPROVED',
            orders: [{ asc: false, column: 'createTime' }],
            ...this.props.paramData
          }
        });
      default:
        return requests('/yst-support/org/emp/list', {
          method: 'post',
          query: {
            ...data,
            empCodeName: data.pcode || data.pname,
            pname: null,
            pcode: null,
            // apprStatus: 'APPROVED',
            orders: [{ asc: false, column: 'createTime' }],
            ...this.props.paramData
          }
        });
    }
  };

  // 分页请求
  request = async (formData) => {
    const param = {
      ...formData,
      orders: [{ asc: false, column: 'createTime' }]
    };

    const res = await this.getList(param);
    if (res.success) {
      let records;
      switch (formData.type || this.props.paramData.ptype) {
        case 'CUST':
          records = res.data.records?.map((item) => ({
            id: item.id,
            pcode: item.custCode,
            pname: item.custName,
            ptype: 'CUST'
          }));
          return {
            ...res,
            data: {
              ...res.data,
              records
            }
          };
        case 'EMP':
          records = res.data.records?.map((item) => ({
            id: item.id,
            pcode: item.empCode,
            pname: item.empName,
            ptype: 'EMP'
          }));
          return {
            ...res,
            data: {
              ...res.data,
              records
            }
          };
        case 'SUPP':
          records = res.data.records?.map((item) => ({
            id: item.id,
            pcode: item.suppCode,
            pname: item.suppName,
            ptype: 'SUPP'
          }));
          return {
            ...res,
            data: {
              ...res.data,
              records
            }
          };
        default:
          records = res.data.records?.map((item) => ({
            id: item.id,
            pcode: item.empCode,
            pname: item.empName,
            ptype: 'EMP'
          }));
          return {
            ...res,
            data: {
              ...res.data,
              records
            }
          };
      }
    } else {
      return res;
    }
  };
  // 分页查询失败回调
  errCallBack = (res) => {
    ElNotification({
      type: 'error',
      message: res.errorMsg || res.msg || res.data || '操作失败！'
    });
  };
  render() {
    return (
      <>
        <PopupSelection
          rowKey='id'
          modalTitle='请选择网格仓名称'
          defaultSearchData={{ type: this.props.paramData?.ptype || 'EMP' }}
          tableProxy={{
            request: this.request,
            successCallBack: () => {},
            errCallBack: this.errCallBack,
            props: {
              success: 'success',
              result: 'data.records',
              total: 'data.total'
            },
            autoLoad: true
          }}
          modalTableColumns={[
            {
              title: '网格仓编码',
              dataIndex: 'pcode',
              width: 160,
              align: 'center'
            },
            {
              title: '网格仓名称',
              dataIndex: 'pname',
              width: 160,
              align: 'center'
            }
          ]}
          columns={[
            {
              title: '网格仓编码',
              dataIndex: 'pcode',
              width: 160,
              align: 'center'
            },
            {
              title: '网格仓名称',
              dataIndex: 'pname',
              width: 160,
              align: 'center'
            }
          ]}
          searchFormProps={{
            items: [
              {
                title: '合作伙伴类型',
                name: 'type',
                span: 8,
                formOption: {
                  type: '$udc',
                  props: {
                    prefixStr: '/yst-system',
                    domain: 'INV',
                    udc: 'PARTNER_TYPE',
                    disabled: this.props.paramData.ptype
                  }
                }
              },
              {
                title: '网格仓编码',
                name: 'pcode',
                span: 8,
                formOption: {
                  type: '$input',
                  props: { placeholder: '请输入网格仓编码' }
                }
              },
              {
                title: '网格仓名称',
                name: 'pname',
                span: 8,
                formOption: {
                  type: '$input',
                  props: { placeholder: '请输入网格仓名称' }
                }
              }
            ]
          }}
          needModal={true}
          onRef={this.onRef}
          value={this.props.value}
          onChange={this.props.onChange}
          overLayWidth={600}
          {...this.props}
        />
      </>
    );
  }
}
export default CoopPartnerPopupSelection;
